using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using TTDotNet.Engine.Industries;

namespace TTDotNet.Engine.Files
{
    public class IndustryLoader : FileLoader
    {

        public IndustryLoader() : base("../../data/industries.xml")
        {

        }

        public Dictionary<IndustryType, IndustryInfo> GetIndustryInfo()
        {
            Dictionary<IndustryType, IndustryInfo> industryInfos = new Dictionary<IndustryType, IndustryInfo>();

            try
            {
                reader.ReadStartElement("Industries");


                while (reader.IsStartElement())
                {
                    IndustryInfo iInfo = new IndustryInfo(reader.GetAttribute("Name"));
                    IndustryType iType = (IndustryType)Enum.Parse(typeof(IndustryType), reader.GetAttribute("Type"));

                    reader.ReadStartElement("Industry");
                    
                    reader.ReadStartElement("Produces");

                    while (reader.Name.Equals("Cargo"))
                    {
                        CargoType type = (CargoType)Enum.Parse(typeof(CargoType), reader.GetAttribute("Name"));
                        iInfo.AddProducingCargo(type);
                        reader.ReadStartElement("Cargo");
                    }

                    reader.ReadEndElement();

                    reader.ReadStartElement("Accepts");

                    while (reader.Name.Equals("Cargo"))
                    {
                        CargoType type = (CargoType)Enum.Parse(typeof(CargoType), reader.GetAttribute("Name"));
                        iInfo.AddAcceptingCargo(type);
                        reader.ReadStartElement("Cargo");
                    }

                    reader.ReadEndElement();

                    reader.ReadStartElement("Meshes");

                    while(reader.Name.Equals("Mesh")) 
                    {
                        string meshPath = reader.GetAttribute("Path");
                        int width = Int32.Parse(reader.GetAttribute("Width"));
                        int length = Int32.Parse(reader.GetAttribute("Length"));
                        string bitmask = reader.GetAttribute("Occupies");

                        BitArray bitArray = new BitArray(bitmask.Length);

                        for (int i = 0; i < bitArray.Length; i++)
                        {
                            bitArray[i] = bitmask.Substring(i, 1).Equals("1");
                        }

                        BuildingInfo bInfo = new BuildingInfo(meshPath, new Size(width, length), bitArray);
                        iInfo.AddBuildingInfo(bInfo);

                        reader.ReadStartElement("Mesh");
                    }

                    reader.ReadEndElement();
                    reader.ReadEndElement();

                    industryInfos[iType] = iInfo;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error parsing XML, " + e.Message);
                Environment.Exit(1);
                return null;
            }

            return industryInfos;

        }
    }
}
